package com.example.websockettest.webSocket.回收站;
import javax.sound.sampled.*;
import java.util.Base64;

public class Main {
    public static final String APP_ID = "31888506";
    public static final String API_KEY = "wLLOPIxZDzSTtTjXLvqyWP8l";
    public static final String SECRET_KEY = "aQ1vWLDT3p18YT33afWZ56DD6ElnrK91";
    public static void main(String[] args) throws Exception {
        String appID = "your app ID";
        String apiKey = "your API Key";
        String secretKey = "your Secret Key";

        ASRClient client = new ASRClient(APP_ID, API_KEY, SECRET_KEY);

        AudioFormat format = new AudioFormat(16000, 8, 1, true, true);
        TargetDataLine line = AudioSystem.getTargetDataLine(format);
        line.open();
        line.start();

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = line.read(buffer, 0, buffer.length)) > 0) {

            // 将音频数据编码为base64格式
            String base64 = Base64.getEncoder().encodeToString(buffer);

            // 调用百度语音识别接口
            String result = client.recognize(base64, bytesRead);
            if (result != null) {
                System.out.println(result);
            }
        }
        }

        //line.stop();
        //line.close();
    }
